<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/27
 * Time: 09:00
 */

namespace Api\Controller;


use Common\Api\AccountApi;
use Common\ConstVar\BoyeActionConstModel;
use Uclient\Model\OAuth2TypeModel;
use Uclient\Model\UcenterMemberModel;

class TaobaoController extends ApiController {

    public function login(){
        // 用户的OpenID
        $openid = $this->_post('openid','','缺少openid');
        $nickname =  $this->_post('nick','');
        $icon_url =  $this->_post('icon_url','');//头像

        //1. 检测是否注册过
        $result = apiCall(AccountApi::GET_INFO_BY_TAOBAO_OPENID,array($openid));

        addLog("Taobao/login",$result,$result,"淘宝登录从数据库获取的用户信息".$openid);
        //如果已经存在，则返回信息
        if($result['status'] && is_array($result['info'])){
            $this->apiReturnSuc($result['info']);
        }elseif($result['status'] === false){
            $this->apiReturnErr($result['info']);
        }


        //2. 注册账户
        $password = sha1($openid);
        $idcode = getIDCode(rand(10000000000,99999999999),'T');

        $entity = array(
            'type'=> UcenterMemberModel::ACCOUNT_TYPE_USERNAME,
            'username' => md5($openid),
            'password' => $password,
            'from' => OAuth2TypeModel::TAOBAO,
            'mobile' => '',
            'realname' => '',
            'nickname' => $nickname ,
            'email' => '',
            'idnumber' => '',
            'birthday' => time(),
            'idcode'=>$idcode,
            'head'=>$icon_url,
            'taobao_openid'=>$openid,
        );

        $result = apiCall(AccountApi::REGISTER, array($entity));

        addLog("Taobao/login",$result,$result,"淘宝登录-注册");
        if($result['status']){
            $uid = $result['info'];
            if($uid > 0){
                $result = apiCall(AccountApi::GET_INFO,array($uid));

                if($result['status']){
                    action_log(BoyeActionConstModel::API_USER_LOGIN, "member", $uid, $uid);
                    $this->apiReturnSuc($result['info']);
                }
            }
        }

        $this->apiReturnErr($result['info']);
    }


}